package Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;


public class DBConnecter {
	
	String driver = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://127.0.0.1:3306/teachingsupportsystem";
	
	String user ="root";
	String password = "654321";
	
	Connection conn = null;
	PreparedStatement preparedStatement = null;
	
	private static final DBConnecter DB_CONNECTER = new DBConnecter() ;
	
	public static DBConnecter getInstance(){
		return DB_CONNECTER;
	}
	
	public Connection getConnecter() {
		try{
			Class.forName(driver);
			conn = DriverManager.getConnection(url,user,password);
		}
		catch(SQLException exception){
			exception.printStackTrace();
		} 
		catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return conn;
	}
	
	public void closeConnecter(Connection connection){
		if(connection!=null){
			try{
				connection.close();
			}
			catch (SQLException exception) {
				exception.printStackTrace();
				// TODO: handle exception
			}
		}
	}
	
	public void closePrepareStatement(PreparedStatement preparedStatement){
		try{
			preparedStatement.close();
		}
		catch (SQLException e) {
			e.printStackTrace();// TODO: handle exception
		}
	}
	
	public void closeResultSet(ResultSet resultSet){
		try{
			resultSet.close();
		}
		catch (SQLException e) {
			e.printStackTrace();// TODO: handle exception
		}
	}

}
